﻿using MusicStore.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace MusicStore.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class SampleHtmlHelperController : Controller
    {
        private MusicStoreDB db = new MusicStoreDB();
        // GET: Assist
        public ActionResult Index()
        {
            var albums = db.Albums.ToList();
            return View();
        }
        public ActionResult Create()
        {
            ViewBag.ArtistId = new SelectList(db.Artists,"ArtistId","Name");
              return View();
        }
        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);//将传入对象album添加至当前集合Albums
                db.SaveChanges();//将数据添加至数据库
                return RedirectToAction("Index");//跳转至Index页面
            }
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
            return View();
        }
        public  ActionResult Eidt(int? id)
        {
            if (id==null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var album = db.Albums.Where(x => x.AlbumId == id).FirstOrDefault();
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        
        }
        [HttpPost]//HttpPost操作选择器
        [ValidateAntiForgeryToken]
        public ActionResult Eidt(Album album)
        {
            //告诉数据上下文，对象在DB已经存在，采用修改模式
            if(ModelState.IsValid)
            {
                db.Entry(album).State = EntityState.Modified;
                album.Genre = db.Genres.Where(x => x.GenreId == album.GenreId).FirstOrDefault();
                //在数据上下文中通过SaveCange方法生成SQL update命令完成对应的记录更新操作
                album.Artist = db.Artists.Where(x => x.ArtistId == album.ArtistId).FirstOrDefault();
                db.SaveChanges();
                return RedirectToAction("Index");

            }
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);

            //album.AlbumId = id;
            //if(ModelState.IsValid)
            //    if(ModelState.IsValid)
            //    {
            //        db.Entry(album).State = EntityState.Modified;
            //        album.Genre = db.Genres.Where(x => x.GenreId == album.GenreId).FirstOrDefault();
            //        album.Artist = db.Artists.Where(x => x.ArtistId == album.ArtistId).FirstOrDefault();
            //        db.SaveChanges();
            //        return RedirectToAction("Index");
            //    }
            //ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            //ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name",album.ArtistId);
            //return View(album);
        }
    }
}